package com.wuming.house.controller;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.House;
import com.wuming.house.model.Secondhouse;
import com.wuming.house.model.WeixinUser;
import com.wuming.house.service.*;
import com.wuming.house.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/house")
public class HouseController extends BaseController {

    @Autowired
    private HouseService houseService;

    @Autowired
    private CateHouseService cateHouseService;

    @Autowired
    private GroupHouseService groupHouseService;

    @Autowired
    private SecondhouseService secondhouseService;

    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String toAdd() {
        return "/house/editHouse";
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult save(House house) {
        try {
            if (null == house.getId()) {
                house.setCreateDatetime(new Date());
                houseService.saveHouse(house);
            } else {
                houseService.modifyHouse(house);
            }
            return new JsonResult<Object>(house, "保存成功.", true);
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return new JsonResult<Object>(null, "保存失败,请联系系统管理员.", false);
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return new JsonResult<Object>(null, "保存失败,请联系系统管理员.", false);
        }
    }


    @RequestMapping(value = "/toManageHouse", method = RequestMethod.GET)
    public String toManageHouse() {
        return "/house/manageHouse";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false) String name, @RequestParam(defaultValue = "1") int pageIndex
            , @RequestParam(defaultValue = "10") int pageSize) {
        try {
            List<House> houses = houseService.getHouseByPage(name, pageIndex, pageSize);
            int total = houseService.getTotal(name);
            return CommonResult.createSuccessResult(houses, total, "查询楼盘成功.");
        } catch (BusinessException be) {
            logger.error("查询楼盘失败.异常:{}", be.getMessage());
            return CommonResult.createFailureResult("查询楼盘失败");
        } catch (Exception e) {
            logger.error("查询楼盘失败.异常:{}", e.getMessage());
            return CommonResult.createFailureResult("查询楼盘失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/listByCate/{cateId}", method = RequestMethod.GET)
    public CommonResult listByCate(@PathVariable Integer cateId, @RequestParam(required = false) String name, @RequestParam(defaultValue = "1") int pageIndex
            , @RequestParam(defaultValue = "10") int pageSize) {
        try {
            if (cateId == 1) {
                List<House> houses = houseService.getHouseByPage(name, pageIndex, pageSize);
                int total = houseService.getTotal(name);
                return CommonResult.createSuccessResult(houses, total, "查询楼盘成功.");
            } else {
                List<Secondhouse> secondhouses = secondhouseService.getSecondhouseByPage(name, pageIndex, pageSize);
                int total = secondhouseService.getTotal(name);
                return CommonResult.createSuccessResult(secondhouses, total, "查询二手房成功");
            }
        } catch (BusinessException be) {
            logger.error("查询楼盘失败.异常:{}", be.getMessage());
            return CommonResult.createFailureResult("查询楼盘失败");
        } catch (Exception e) {
            logger.error("查询楼盘失败.异常:{}", e.getMessage());
            return CommonResult.createFailureResult("查询楼盘失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult detele(@PathVariable Long id) {
        logger.info("准备删除楼盘信息ID:{}", id);
        try {
            //删除新房分组中的存在该新房的数据
            groupHouseService.deleteByHouseId(id);
            //删除新房分类中的存在该新房的数据
            cateHouseService.deleteByHouseId(id);
            houseService.removeHouseById(id);
            return CommonResult.createSuccessResult("删除楼盘信息成功");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("删除楼盘信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("删除楼盘信息失败.");
        }
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String toEdit(@PathVariable Long id, Model model) {
        logger.info("准备修改楼盘信息ID:{}", id);
        House house = houseService.getHouseById(id);
        model.addAttribute("house", house);
        return "/house/editHouse";
    }


    @RequestMapping(value = "/toList/{id}/{type}/{groupType}", method = RequestMethod.GET)
    public String toList(@PathVariable Integer id, @PathVariable Integer type, @PathVariable Integer groupType,
                         Model model) {
        if (type == 1) {
            model.addAttribute("groupId", id);
            if (groupType == 1) {
                return "/house/manageGroupHouse";
            }
            return "/secondhouse/manageSecondGroupHouse";
        } else {
            model.addAttribute("cateId", id);
            return "/house/manageCateHouse";
        }
    }

    @RequestMapping(value = "/manageImg/{houseId}", method = RequestMethod.GET)
    public String manageImg(@PathVariable Long houseId, @RequestParam Integer houseType, Model model) {
        model.addAttribute("houseType", houseType);
        model.addAttribute("houseId", houseId);
        return "/house/manageImg";
    }

    @RequestMapping(value = "/manageLayout/{houseId}", method = RequestMethod.GET)
    public String manageImg(@PathVariable Long houseId, Model model) {
        model.addAttribute("houseId", houseId);
        return "/house/manageLayout";
    }

    @RequestMapping(value = "/manageInfo/{houseId}", method = RequestMethod.GET)
    public String manageInfo(@PathVariable Long houseId, Model model) {
        model.addAttribute("houseId", houseId);
        return "/house/manageInfo";
    }

    @RequestMapping(value = "/manageTag/{houseId}", method = RequestMethod.GET)
    public String manageTag(@PathVariable Long houseId, Model model) {
        model.addAttribute("houseId", houseId);
        return "/house/manageTag";
    }

    @RequestMapping(value = "/manageSecondhouseprice", method = RequestMethod.GET)
    public String manageSecondhouseprice() {
//        model.addAttribute("houseId", houseId);
        return "/house/manageSecondhouseprice";
    }
}
